package com.xsm.activitygroup;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.xsm.activitygroup.R;
import com.xsm.internet.PostMessage;
import com.xsm.view.LoadDialog;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class EditPassword extends Activity{
	com.xsm.view.ClearEditText oldpassword,newpassword,rpassword;
	String oldString="",newString="",rString="";
	Context context;
	String user;
	LoadDialog dialog;
	myHandler handler;
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.editpassword);
	context=this;
	oldpassword=(com.xsm.view.ClearEditText)findViewById(R.id.oldpassword);
	newpassword=(com.xsm.view.ClearEditText)findViewById(R.id.newpassword);
	rpassword=(com.xsm.view.ClearEditText)findViewById(R.id.rpassword);
	 handler=new  myHandler();
	SharedPreferences sharedPreferences=getSharedPreferences("XSM", 0);
	user=sharedPreferences.getString("User","");
	dialog=new LoadDialog(context,"正在提交...");
}

public void Back(View view){
	finish();
}

public void Edit(View view){
	oldString=oldpassword.getText().toString().trim();
	newString=newpassword.getText().toString().trim();
	rString=rpassword.getText().toString().trim();
	
	if(oldString.equals("")||newString.equals("")||rString.equals("")){
		Toast.makeText(context, "请填写完整信息", Toast.LENGTH_LONG).show();
		return;
	}
	if(!newString.equals(rString))
	{
		Toast.makeText(context, "两次密码输入不一致", Toast.LENGTH_LONG).show();
		return;
	}
	if(newString.length()<6||rString.length()<6){
		Toast.makeText(context, "密码至少6位", Toast.LENGTH_LONG).show();
		return;
	}
	
	dialog.showDialog();
	new Thread(new Runnable() {
		public void run() {
			List params=new ArrayList();
			params.add(new BasicNameValuePair("ASK","EditPassword"));
			params.add(new BasicNameValuePair("user",user));
			params.add(new BasicNameValuePair("oldpassword",oldString));
			params.add(new BasicNameValuePair("newpassword",newString));
			PostMessage postMessage=new PostMessage(params);
			String result=postMessage.getResult();
			Message msg=handler.obtainMessage();
			if(result.equals("-1"))
			{
				msg.what=-1;
			}else if(result.equals("0")){
				msg.what=0;
			}else {
				msg.what=1;
			}
			
			handler.sendMessage(msg);
		}
	}).start();
	
}


class myHandler extends Handler{
	public void handleMessage(Message msg) {
		dialog.dissmissDialog();
if(msg.what==0){
	Toast.makeText(context, "原始密码不正确", Toast.LENGTH_LONG).show();
}else if(msg.what==-1){
	Toast.makeText(context, "连接服务器失败,请检查网络配置", Toast.LENGTH_LONG).show();
}else {
	Toast.makeText(context, "修改成功", Toast.LENGTH_LONG).show();
}
super.handleMessage(msg);
	}
}

}
